/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sino.dataworks.demos.web;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.setting.dialect.Props;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dataworks_public.model.v20200518.ListInstancesRequest;
import com.aliyuncs.dataworks_public.model.v20200518.ListInstancesResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.utils.StringUtils;
import com.sino.dataworks.cache.PhoneCache;
import com.sino.dataworks.dto.InstanceEntityDto;
import com.sino.dataworks.entity.DwDutyRoster;
import com.sino.dataworks.enums.DataWorksTaskStatus;
import com.sino.dataworks.enums.ProjectEnums;
import com.sino.dataworks.service.DwDutyRosterService;
import com.sino.dataworks.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.*;

import static com.sino.dataworks.util.JobVarUtil.*;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@Controller
@Slf4j
public class BasicController {

    static IAcsClient client = null;
    static Map<String, ListInstancesResponse.Data.Instance> instanceMap = new HashMap<>();
    @Autowired
    DwDutyRosterService dwDutyRosterService;
    static {
        String endPoint = System.getenv("ALIBABA_END_POINT");
        String regionId = System.getenv("ALIBABA_REGION_ID");
        if (StringUtils.isEmpty(endPoint)) {
            endPoint = "dataworks-public-vpc.res.cloud.cheryfs.cn";
        }
        String keyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String keySec = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        if (StringUtils.isEmpty(keyId)) {
            keyId = "ZR18OFackif3jKT7";
        }
        if (StringUtils.isEmpty(keySec)) {
            keySec = "Pfvaw2BOmDPC4zEu1UglDu87jCn5yi";
        }
        if (StringUtils.isEmpty(regionId)) {
            regionId = "cn-wuhu-qrhy-d01";
        }
        DefaultProfile profile = DefaultProfile.getProfile(
                regionId,
                keyId, // 替换为你的AccessKey ID
                keySec); // 替换为你的AccessKey Secret
        DefaultProfile.addEndpoint(regionId, "dataworks-public", endPoint);
        client = new DefaultAcsClient(profile);
    }
    public void failureInstances(Integer total) {
        int totalPage=total/100+1;
        for (int i=1;i<=totalPage;i++){
            getData(i);
        }
    }

    public String getData(int page) {
        // 创建DefaultAcsClient实例并初始化
        StringBuffer stringBuffer = new StringBuffer();
        Date newDate = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -1);
        String startTime = DateUtil.format(newDate, "yyyy-MM-dd") + " 00:00:00";
        String endTime = DateUtil.format(newDate, "yyyy-MM-dd") + " 00:00:00";
        String time = DateUtil.format(newDate, "yyyyMMdd");
        // 创建API请求并设置参数
        ListInstancesRequest request = new ListInstancesRequest();
        request.setProjectId(10022L); // 替换为你的DataWorks项目名称
        request.setProjectEnv("PROD");
        request.setBeginBizdate(startTime);
        request.setEndBizdate(endTime);
        request.setPageSize(100);
        request.setPageNumber(page);
        // 可以根据需要设置更多参数，如pageNumber、pageSize等
        try {
            // 调用ListInstances方法
            ListInstancesResponse response = client.getAcsResponse(request);
            // 处理响应结果
            if (response != null && response.getSuccess()) {
                stringBuffer.append("第" + page + "页：" + JSON.toJSONString(response) + "\n");
                List<ListInstancesResponse.Data.Instance> instances = response.getData().getInstances();
                instances.forEach(instance -> {
                    if (instance.getStatus().equals("FAILURE")) {
                        log.info("失败实例:"+ instance.getInstanceId()+":实例名称:"+instance.getNodeName());
                    }
                    log.info("实例ID:"+ instance.getInstanceId()+":实例名称:"+instance.getNodeName());
                });
            } else {
                log.info("Failed to list instances. Error code: " + response.getErrorCode() + ", Error message: " + response.getErrorMessage());
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }


    @Autowired
    private DwDutyRosterService service;

    // http://127.0.0.1:8080/hello?name=lisi
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
        PhoneCache.initPhone();
       // failureInstances(100);
        if(name.equals("unknown user")){
            DataworksUtil.login();
        }
//       Long total=DataworksUtil.getMetaDBTableList(1);
//        int page=total.intValue()+100-1/100;
//        System.out.println("总条数:"+total);
//        for (int i=1;i<=page;i++){
//            DataworksUtil.getMetaDBTableList(i);
//            DataworksUtil.grantTable();
//            try {
//                Thread.sleep(1000*60);
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        }
//        List<Record> records=DataworksUtil.getRecordByNodeId("15585");
//       // for (Record record:records){
//            DataworksUtil.insertInstanceLog("20240704",records,"dw_instance_task_info_log");
//     //   }
//        DataworksUtil.getSyncTaskDetail(1);
//        Integer totalNum= JobVarUtil.instaceListTotalCount;
//        int totalPages=(totalNum+100-1)/100;
//        for (int i=2;i<=totalPages;i++){
//            DataworksUtil.getSyncTaskDetail(i);
//        }

        for (ProjectEnums enums:ProjectEnums.values()){
            DataworksUtil.getDatasource(enums.getCode());
            DataworksUtil.getTaskLog(1,enums.getCode());
        }
      //  DataworksUtil.getRecords(name);
   //     DataworksUtil.getTaskLog();
//        insertInstanceLog(name, DataworksUtil.getRecords(name), "dw_instance_task_info_log");
//        DataworksUtil.getRecords(1,name);
//        Integer total= JobVarUtil.totalCount;
//        log.info("总条数:"+total);
//        int totalPage=(total+100-1)/100;
//        log.info("总页数:"+totalPage);
//        int pageNum=0;
//        while (pageNum<=30){
//            pageNum++;
//            insertInstanceLog(name, DataworksUtil.getRecords(pageNum, name), "dw_instance_task_info_log");
//        }
//        for(int i=1;i<=totalPage;i++) {
//            insertInstanceLog(name, DataworksUtil.getRecords(i, name), "dw_instance_task_info_log");
//        }
         //    JschUtil.e
       // SendMailUtil.sendMail("","", "数据中台跑批核对", sb.toString());
        JobVarUtil.instanceMap.clear();
        JobVarUtil.instaceListTotalCount=0;
        JobVarUtil.instanceSyncTaskMap.clear();
        JobVarUtil.mailRecords=new ArrayList<>();
      //  DataworksUtil.phoneWarning();
        return JSON.toJSONString(PhoneCache.phoneCache);
    }

    @RequestMapping("/getTable")
    @ResponseBody
    public String getTable(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
       Long total=DataworksUtil.getMetaDBTableList(1);
        int page=total.intValue()+100-1/100;
        System.out.println("总条数:"+total);
        for (int i=1;i<=page;i++){
            DataworksUtil.getMetaDBTableList(i);
            DataworksUtil.grantTable();
        }
         applyList=new ArrayList<>();
//        List<Record> records=DataworksUtil.getRecordByNodeId("15585");
//       // for (Record record:records){
//            DataworksUtil.insertInstanceLog("20240704",records,"dw_instance_task_info_log");
//     //   }
        return JSON.toJSONString(PhoneCache.phoneCache);
    }

    @RequestMapping("/getTableLine")
    @ResponseBody
    public String getTableLine(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
        List<MultiSheetExcelExporter.SheetMeta> allSheets = new ArrayList<>();
        if(name.indexOf(",")>0){
            for (String s:name.split(",")){
                log.info("表名:"+s);
                MultiSheetExcelExporter.SheetMeta data=DataworksUtil.getMetaTableColumn(s);
                allSheets.add(data);
            }
        }else{
            MultiSheetExcelExporter.SheetMeta data=DataworksUtil.getMetaTableColumn(name);
            allSheets.add(data);
        }
        log.info(JSON.toJSONString(allSheets));
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.setDestFile(new java.io.File("output.xlsx"));
        MultiSheetExcelExporter.createCatalogSheet(writer, allSheets);
        for (int i = 0; i < allSheets.size(); i++) {
            MultiSheetExcelExporter.SheetMeta sheetMeta = allSheets.get(i);
            MultiSheetExcelExporter.createDataSheet(writer, sheetMeta, i+1); // +1因为目录是第一个sheet
        }
        MultiSheetExcelExporter.setGlobalStyle(writer);
        writer.close();
        return JSON.toJSONString(PhoneCache.phoneCache);
    }

    @PostMapping("/getTableLineAll")
    @ResponseBody
    public String getTableLineAll(@RequestBody String[] names) {
        List<MultiSheetExcelExporter.SheetMeta> allSheets = new ArrayList<>();
            for (String s:names){
                MultiSheetExcelExporter.SheetMeta data=DataworksUtil.getMetaTableColumn(s);
                allSheets.add(data);
            }
//        }else{
//            MultiSheetExcelExporter.SheetMeta data=DataworksUtil.getMetaTableColumn(name);
//            allSheets.add(data);
//        }
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.setDestFile(new java.io.File("output.xlsx"));
        MultiSheetExcelExporter.createCatalogSheet(writer, allSheets);
        for (int i = 0; i < allSheets.size(); i++) {
            MultiSheetExcelExporter.SheetMeta sheetMeta = allSheets.get(i);
            MultiSheetExcelExporter.createDataSheet(writer, sheetMeta, i+1); // +1因为目录是第一个sheet
        }
        MultiSheetExcelExporter.setGlobalStyle(writer);
        writer.close();
        return JSON.toJSONString(PhoneCache.phoneCache);
    }
    @PostMapping("/bushu")
    @ResponseBody
    public String bushu(@RequestBody JSONObject jsonObject) {
        return DataworksUtil.bushu(jsonObject);
    }
    @RequestMapping("/loadConfig")
    @ResponseBody
    public String loadConfig() {
        Props props = new Props(new File("config.properties"));
        JobVarUtil.config=props;
        log.info("重新加载配置文件:"+ JSON.toJSONString(props));
        return JSON.toJSONString(props);
    }
    @RequestMapping("/errorLog")
    @ResponseBody
    public String errorLog(@RequestParam(name = "nodeId", defaultValue = "") String nodeId) {
        String errorLog=DataworksUtil.getInstanceLog(DataworksUtil.getInstancesByNodeId(nodeId).get(0).getInstanceId());
        List<String> tableNames=  DataworksUtil.parseLogForTableNotFound(errorLog);
        if(tableNames!=null&&tableNames.size()>0){
            for (String tableName:tableNames){
                DataworksUtil.submitTable(tableName.replace("datawarehouse_pro.",""));
            }
        }
        return errorLog;
    }

    @RequestMapping("/checkFile")
    @ResponseBody
    public String checkFile(@RequestParam(name = "fileName", defaultValue = "") String fileName,@RequestParam(name = "bizDate", defaultValue = "") String bizDate) {
        Boolean flag=DataworksUtil.checkFile(fileName,bizDate);
        return String.valueOf(flag);
    }

    @RequestMapping("/modifyJobTime")
    @ResponseBody
    public String modifyJobTime(@RequestParam(name = "jobName", defaultValue = "dataworksPhoneWarningTask") String jobName,@RequestParam(name = "time", defaultValue = "0 0/10 0-6 * * ?") String time) {
        QuartzManagerUtil.modifyJobTime(jobName,jobName,time);
        QuartzManagerUtil.getJobDetail();
        return "";
    }

    @RequestMapping("/table")
    @ResponseBody
    public String table(@RequestParam(name = "table", defaultValue = "") String table) {
        String tableDesc=DataworksUtil.submitTable(table);
        return tableDesc;
    }
    @RequestMapping("/insertLog")
    @ResponseBody
    public String insertLog(@RequestParam(name = "bizdate", defaultValue = "unknown user") String bizdate) {
        DataworksUtil.getSyncTaskDetail(1);
        Integer totalNum= JobVarUtil.instaceListTotalCount;
        int totalPages=(totalNum+100-1)/100;
        for (int i=2;i<=totalPages;i++){
            DataworksUtil.getSyncTaskDetail(i);
        }
        DataworksUtil.getRecords(bizdate);
        JobVarUtil.instanceMap.clear();
        JobVarUtil.instaceListTotalCount=0;
        JobVarUtil.instanceSyncTaskMap.clear();
        JobVarUtil.mailRecords=new ArrayList<>();
        JobVarUtil.retryCount=0;
        return JSON.toJSONString(PhoneCache.phoneCache);
    }
    @RequestMapping("/login")
    @ResponseBody
    public String login() {
        DataworksUtil.login();
        return JSON.toJSONString(PhoneCache.phoneCache);
    }
    @RequestMapping("/phone")
    @ResponseBody
    public String phone() {
        DataworksUtil.phoneWarning(null);
        return JSON.toJSONString(PhoneCache.phoneCache);
    }
    @RequestMapping("/checkOracle")
    @ResponseBody
    public String checkOracle() {
        Date newDate = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -1);
        String bizDate = DateUtil.format(newDate, "yyyy-MM-dd");
        String endTime = DateUtil.format(new Date(), "yyyy-MM-dd");
        List<InstanceEntityDto> list=DataworksUtil.checkOracleScript(bizDate,endTime);
        return JSON.toJSONString(list);
    }
    @RequestMapping("/search")
    @ResponseBody
    public String search(@RequestParam(name = "search", defaultValue = "unknown user") String search) {
        List<InstanceEntityDto> list=new ArrayList<>();
        Date newDate = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -1);
        String bizDate = DateUtil.format(newDate, "yyyy-MM-dd");
        String endTime = DateUtil.format(new Date(), "yyyy-MM-dd");
        for(int i=1;i<30;i++){
            List<InstanceEntityDto> dtos=DataworksUtil.getInstance(search,bizDate,endTime,i,100);
            if(dtos!=null&&dtos.size()>0){
                list.addAll(dtos);
            }
        }
        List<String> nodeList=new ArrayList<>();
        for (InstanceEntityDto dto:list){
            nodeList.add(dto.getNodeName());
        }
        FileUtil.writeLines(nodeList,"./node.txt","UTF-8");
        ExcelWriter writer = ExcelUtil.getWriter("./node.xlsx");
        writer.setOnlyAlias(true);
        writer.write(list, true);
        writer.close();
        return search;
    }
    @RequestMapping("/searchNode")
    @ResponseBody
    public String searchNode(@RequestBody Map<String,String> param) {
        List<InstanceEntityDto> list=new ArrayList<>();
        DataworksUtil.getNodeList(param);
        Integer totalNum= JobVarUtil.nodeListTotalCount;
        log.info("总条数:"+totalNum);
        int totalPages=(totalNum+100-1)/100;
        for (int i=1;i<=totalPages;i++){
            param.put("pageNum",i+"");
            List<InstanceEntityDto> dtos=DataworksUtil.getNodeList(param);
            if(dtos!=null&&dtos.size()>0){
                list.addAll(dtos);
            }
        }
        List<String> nodeList=new ArrayList<>();
        for (InstanceEntityDto dto:list){
            nodeList.add(dto.getNodeName());
        }
        FileUtil.writeLines(nodeList,"./node.txt","UTF-8");
        ExcelWriter writer = ExcelUtil.getWriter("./node.xlsx");
        writer.addHeaderAlias("nodeName", "节点名称");
        writer.addHeaderAlias("nodeId", "节点ID");
        writer.addHeaderAlias("createUser", "创建人");
        writer.setOnlyAlias(true);
        writer.write(list, true);
        writer.close();
        JobVarUtil.nodeListTotalCount=0;
        return JSON.toJSONString(param);
    }

    @RequestMapping("/getDataBase")
    @ResponseBody
    public String getDataBase(@RequestBody Map<String,String> param) {
        DataworksUtil.getDatabase("100",1);
        Integer databaseTotalNum= JobVarUtil.databaseTotalCount;
        Integer databaseTotalPages=(databaseTotalNum+100-1)/100;
        if(databaseTotalPages>1){
            for (int i=2;i<=databaseTotalPages;i++){
                DataworksUtil.getDatabase("100",i);
            }
        }
        DataworksUtil.getDataSourceData(param.get("bizDate"),param.get("tableName"),param.get("project"));
        return JSON.toJSONString(param);
    }
    @RequestMapping("/getBanlance")
    @ResponseBody
    public String getBanlance() {
        return DataworksUtil.getLoadBalancers();
    }

    @RequestMapping("/getAllBySearch")
    @ResponseBody
    public String getAllBySearch(@RequestBody Map<String,String> param) {
        List<InstanceEntityDto> list=new ArrayList<>();
        DataworksUtil.getNodeList(param);
        DataworksUtil.getDatabase("100",1);
        Integer databaseTotalNum= JobVarUtil.databaseTotalCount;
        Integer databaseTotalPages=(databaseTotalNum+100-1)/100;
        if(databaseTotalPages>1){
            for (int i=2;i<=databaseTotalPages;i++){
                DataworksUtil.getDatabase("100",i);
            }
        }
        Integer totalNum= JobVarUtil.nodeListTotalCount;
        log.info("总条数:"+totalNum);
        int totalPages=(totalNum+100-1)/100;
        for (int i=1;i<=totalPages;i++){
            param.put("pageNum",String.valueOf(i));
            List<InstanceEntityDto> dtos=DataworksUtil.getNodeList(param);
            if(dtos!=null&&dtos.size()>0){
                list.addAll(dtos);
            }
        }
        List<Map<String,String>> list1=new ArrayList<>();
        for (InstanceEntityDto dto:list){
            Integer nodeId=dto.getNodeId();
            String owerName=dto.getOwnerName();
            String cycTypeName=dto.getCycTypeName();
            String prgTypeName=  dto.getPrgTypeName();
            Map<String,String> map=new HashMap<>();
            if(nodeId!=null&&prgTypeName.equals("离线同步")){
                String data= DataworksUtil.getNodeCode(String.valueOf(nodeId));
                JSONArray jsonObject=JSONObject.parseObject(data).getJSONArray("steps");
                for (int i=0;i<jsonObject.size();i++){
                    JSONObject jsonObject1=jsonObject.getJSONObject(i);
                    JSONObject js=jsonObject1.getJSONObject("parameter");
                    String name=jsonObject1.getString("name");
                    map.put("owerName",owerName);
                    if(name.equals("Reader")){
                        map.put("table",js.getString("table"));
                        map.put("comment",js.getString("tableComment"));
                    }
                    if(name.equals("Writer")){
                        map.put("destable",js.getString("table"));
                        map.put("type",jsonObject1.getString("stepType").toUpperCase());
                        String database=js.getString("datasource");
                        String description=null;
                        if(datasourceMap.containsKey(database)){
                            description=datasourceMap.get(database).getString("description");
                        }
                        map.put("database",StringUtils.isNotEmpty(description)?description:database);
                    }
                    map.put("name",name);
                    map.put("cycTypeName",cycTypeName);
                }
            }
            if(map.size()>0){
                list1.add(map);
            }
        }
        ExcelWriter writer = ExcelUtil.getWriter(true);
        writer.setDestFile(new java.io.File("推送清单.xlsx"));
        writer.writeRow(Arrays.asList(
                "dataworks表名", "中文名", "推送系统","推送数据库类型",
                "推送表名", "推送频次","创建人"
        ));
        for (Map<String, String> field : list1) {
            writer.writeRow(Arrays.asList(
                    field.get("table"),
                    field.get("comment"),
                    field.get("database"),
                    field.get("type"),
                    field.get("destable"),
                    field.get("cycTypeName"),
                    field.get("owerName")
            ));
        }
        writer.autoSizeColumnAll();
        writer.close();
        JobVarUtil.nodeListTotalCount=0;
        JobVarUtil.databaseTotalCount=0;
        return JSON.toJSONString(param);
    }

    @RequestMapping("/checkColumn")
    @ResponseBody
    public String checkColumn(@RequestParam(name = "searchText", defaultValue = "_integration") String searchText) {
        List<InstanceEntityDto> list=new ArrayList<>();
        Map<String,String> param=new HashMap<>();
        param.put("searchText",searchText);
        param.put("pageNum","1");
        param.put("pageSize","100");
        DataworksUtil.getNodeList(param);
        DataworksUtil.getDatabase("100",1);
        Integer totalNum= JobVarUtil.nodeListTotalCount;
        log.info("总条数:"+totalNum);
        int totalPages=(totalNum+100-1)/100;
        for (int i=1;i<=totalPages;i++){
            param.put("pageNum",String.valueOf(i));
            List<InstanceEntityDto> dtos=DataworksUtil.getNodeList(param);
            if(dtos!=null&&dtos.size()>0){
                list.addAll(dtos);
            }
        }
        List<Map<String,JSONObject>> list1=new ArrayList<>();
        for (InstanceEntityDto dto:list){
            Integer nodeId=dto.getNodeId();
            String nodeName=dto.getNodeName();
            String owerName=dto.getOwnerName();
            String cycTypeName=dto.getCycTypeName();
            String prgTypeName=  dto.getPrgTypeName();
            Map<String,JSONObject> map=new HashMap<>();
            if(nodeId!=null&&prgTypeName.equals("离线同步")&&nodeName.startsWith("ods_")){
                String data= DataworksUtil.getNodeCode(String.valueOf(nodeId));
                JSONArray jsonObject=JSONObject.parseObject(data).getJSONArray("steps");
                int readerCount=0;
                int writerCount=0;
                for (int i=0;i<jsonObject.size();i++){
                    JSONObject jsonObject1=jsonObject.getJSONObject(i);
                    JSONObject js=jsonObject1.getJSONObject("parameter");
                    String name=jsonObject1.getString("name");
                    if(name.equals("Reader")){
                        map.put("reader",jsonObject1);
                    }
                    if(name.equals("Writer")){
                        map.put("writer",jsonObject1);
                    }
                }
                JSONObject jsonO=new JSONObject();
                jsonO.put("fileId",dto.getFileId());
                jsonO.put("nodeId",nodeId);
                jsonO.put("nodeName",nodeName);
                map.put("fileId",jsonO);
                map.put("data",JSON.parseObject(data));
            }
            if(map.size()>0){
                list1.add(map);
            }
        }
        DataworksUtil.checkColumn(list1);
        return JSON.toJSONString(param);
    }
    @RequestMapping("/notRun")
    @ResponseBody
    public String notRun(@RequestParam(name = "num", defaultValue = "600") String num) {
        JobVarUtil.notRunNum=Integer.valueOf(num);
        return JSON.toJSONString(JobVarUtil.notRunNum);
    }
    @RequestMapping("/setSuccess")
    @ResponseBody
    public String setSuccess() {
        List<InstanceEntityDto> dtos=new ArrayList<>();
        Date newDate = DateUtil.offset(new Date(), DateField.DAY_OF_MONTH, -1);
        String bizDate = DateUtil.format(newDate, "yyyy-MM-dd");
        String endTime = DateUtil.format(new Date(), "yyyy-MM-dd");
        dtos= DataworksUtil.getInstance(DataWorksTaskStatus.FAILURE.getCode(),bizDate,endTime);
        for(InstanceEntityDto dto:dtos){
            String setSuccess= config.getStr("setSuccess");
            if(StringUtils.isNotEmpty(setSuccess)&&Arrays.asList(setSuccess).contains(dto.getNodeName())){
                Long instanceId=dto.getTaskId();
                String errorLogs=DataworksUtil.getInstanceLog(dto.getTaskId());
                String logDesc= LogParserUtil.getlogErrorInfo(errorLogs);
                if(logDesc.contains("overflows to convert to")){
                    DataworksUtil.setSuccessInstance(instanceId);
                }
            }
        }
        return JSON.toJSONString(JobVarUtil.notRunNum);
    }
    @RequestMapping("/importExcel")
    @ResponseBody
    public String importExcel(@RequestParam(name = "tableName", defaultValue = "") String tableName,@RequestParam(name = "project", defaultValue = "datawarehouse_pro") String project,@RequestParam(name = "ds", defaultValue = "") String ds) {
        DataworksUtil.importExcel(tableName,project,ds);
        return "成功";
    }
    @RequestMapping("/duty")
    @ResponseBody
    public String generateDutyRoster(@RequestParam(name = "start", defaultValue = "2024-01-01") String start,@RequestParam(name = "end", defaultValue = "2024-12-31") String end) {
        List<DwDutyRoster>  dutyRosters=DutyRosterUtil.generateDutyRoster(start,end);
        dwDutyRosterService.save(dutyRosters);
        return JSON.toJSONString(dutyRosters);
    }
    @RequestMapping("/hello2")
    @ResponseBody
    public String hello2(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
        DataworksUtil.phoneWarning(null);
        return JSON.toJSONString(PhoneCache.phoneCache);
    }
    @RequestMapping("/hello3")
    @ResponseBody
    public String hello3() {
       // DataworksUtil.runManualDagNodes(JobVarUtil.bizDate,"mpp临时抽数","datawarehouse_pro","27672",10022L);
        return "1234";
    }
    @RequestMapping("/getNode")
    @ResponseBody
    public String getNode(@RequestParam(name = "nodeId", defaultValue = "unknown user") String nodeId) {
        return JSON.toJSONString(DataworksUtil.getInstancesByNodeId(nodeId));
    }
    @RequestMapping("/getLog")
    @ResponseBody
    public String getLog(@RequestParam(name = "instanceId", defaultValue = "unknown user") String instanceId) {
        return JSON.toJSONString(DataworksUtil.getInstanceLog(Long.valueOf(instanceId)));
    }
    @RequestMapping("/gps")
    @ResponseBody
    public String gps(@RequestParam(name = "bizdate", defaultValue = "") String bizdate) {
       Long total= DataworksUtil.gpsMonitor(bizdate,"device_history_kafka","gps_project");
        return String.valueOf(total);
    }

    @RequestMapping("/getNodeCode")
    @ResponseBody
    public String getNodeCode(@RequestParam(name = "nodeId", defaultValue = "unknown user") String nodeId) {
        return DataworksUtil.getNodeCode(nodeId);
    }
    @RequestMapping("/GetInstance")
    @ResponseBody
    public String GetInstance(@RequestParam(name = "nodeId", defaultValue = "unknown user") String nodeId) {
        DataworksUtil.getInstanceByNodeId(nodeId);
        return "aa" ;
    }

    // http://127.0.0.1:8080/user
    @RequestMapping("/user")
    @ResponseBody
    public User user() {
        User user = new User();
        user.setName("theonefx");
        user.setAge(666);
        PhoneCache.phoneCache.clear();
        return user;
    }

    // http://127.0.0.1:8080/save_user?name=newName&age=11
    @RequestMapping("/save_user")
    @ResponseBody
    public String saveUser(User u) {
        return "user will save: name=" + u.getName() + ", age=" + u.getAge();
    }

    // http://127.0.0.1:8080/html
    @RequestMapping("/html")
    public String html(){
        return "index.html";
    }

    @ModelAttribute
    public void parseUser(@RequestParam(name = "name", defaultValue = "unknown user") String name
            , @RequestParam(name = "age", defaultValue = "12") Integer age, User user) {
        user.setName("zhangsan");
        user.setAge(18);
    }

}
